package com.spring.demo.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.spring.demo.pojo.Book;
import com.spring.demo.service.IBookService;

/**
 *
 * @author nickel
 * @date 2019-01-24 15:39:46
 */
@Controller
public class HelloController {

	private static Logger log = LoggerFactory.getLogger(HelloController.class);

	// TODO 根目录下有index.jsp页面时，该请求没有被处理，会优先访问index.jsp
	@RequestMapping("/")
	public String home() {
		log.info("home");
		return "home";
	}

	@Autowired
	private IBookService bookService;
	
	@PostMapping("/test")
	public ModelAndView postTest(ModelAndView mv, String name, Integer age) {
		bookService.save(new Book(name, name));
		List<Book> list = bookService.list();
		mv.addObject("books", list);
		mv.setViewName("redirect:/book/list");
		return mv;
	}
}
